import time
import cv2
import numpy as np
import Processor.Converter as Converter

from PyQt5.QtWidgets import QDialog
from Plugin.colormap.colormap_design import Ui_ColormapController


ColormapName = {
    cv2.COLORMAP_AUTUMN: '秋天',
    cv2.COLORMAP_BONE: '骨质 ',
    cv2.COLORMAP_JET: '热力图',
    cv2.COLORMAP_WINTER: '冬天',
    cv2.COLORMAP_RAINBOW: '彩虹',
    cv2.COLORMAP_OCEAN: '海洋',
    cv2.COLORMAP_SUMMER: '夏天',
    cv2.COLORMAP_SPRING: '春天',
    cv2.COLORMAP_COOL: '冷色',
    cv2.COLORMAP_HSV: 'HSV',
    cv2.COLORMAP_PINK: '粉色',
    cv2.COLORMAP_HOT: '热',
    cv2.COLORMAP_PARULA: '森莺',
    cv2.COLORMAP_MAGMA: '岩浆',
    cv2.COLORMAP_INFERNO: '地狱火',
    cv2.COLORMAP_PLASMA: '等离子体',
    cv2.COLORMAP_VIRIDIS: '翡翠',
    cv2.COLORMAP_CIVIDIS: 'Cividis',
    cv2.COLORMAP_TWILIGHT: '暮色',
    cv2.COLORMAP_TWILIGHT_SHIFTED: '暮色 - Shifted'
}


class ColormapController(QDialog, Ui_ColormapController):
    layer = None  # 图层
    mat = None  # 原始Mat 数据
    colormapName = ColormapName # 颜色表名字

    def __init__(self, parent, mat=None):
        super().__init__(parent)
        self.setupUi(self)
        self.mat = mat
        self.initUI(parent)
        self.colormapName = ColormapName

    def reject(self):
        """
        取消操作
        :return:
        """
        image = Converter.Numpy2QImage(self.mat)
        self.layer.updatePixmap(image)
        QDialog.reject(self)

    def initUI(self, parent):
        self.setWindowTitle('伪色图')
        self.custom_pannel.hide()
        # 定位
        self.resize(160, 160)
        pos = parent.geometry()
        self.move((pos.width() - self.width()) / 2, (pos.height() - self.height()) / 2)
        # 添加色图
        for i in range(0, len(self.colormapName)):
            self.comboBox_preset.addItem(self.colormapName[i])
        #           信号
        # 对话框确定
        self.DialogButtonBox.accepted.connect(self.accept)
        self.DialogButtonBox.rejected.connect(self.reject)
        # 参数
        self.comboBox_preset.currentIndexChanged.connect(self.updateSlot)
        self.radioButton_custom.toggled.connect(self.__radioButton_custom_toggled_slot)

    def __radioButton_custom_toggled_slot(self):
        if self.radioButton_custom.isChecked():
            self.custom_pannel.show()
            colormap = self.custom_pannel.toColormap()
            self.applyCustomColormap(colormap)
        else:
            self.custom_pannel.hide()
            self.updateSlot()

    def updateSlot(self):
        colormapType = self.comboBox_preset.currentIndex()
        self.__applyColormap(colormapType)

    def __applyColormap(self, colormapType):
        """
        应用色图变换
        :param colormapType: 色图类型
        :return:
        """
        start = time.perf_counter()
        mat = self.mat.copy()
        mat[:, :, 0:3] = cv2.applyColorMap(mat[:, :, 0:3], colormapType)
        image = Converter.Numpy2QImage(mat)
        end = time.perf_counter()
        print('applyColorMap', '耗时：', end - start)
        self.layer.updatePixmap(image)

    def applyCustomColormap(self, colormap):
        start = time.perf_counter()
        mat = self.mat.copy()
        mat[:, :, 0] = cv2.LUT(mat[:, :, 0], colormap[0])
        mat[:, :, 1] = cv2.LUT(mat[:, :, 1], colormap[1])
        mat[:, :, 2] = cv2.LUT(mat[:, :, 2], colormap[2])
        image = Converter.Numpy2QImage(mat)
        end = time.perf_counter()
        print('applyCustomColormap', '耗时：', end - start)
        self.layer.updatePixmap(image)


Window = None  # 主窗体


def init(window):
    """
    初始化
    :param window: 主窗体
    :return:
    """
    global Window
    Window = window
    __menu()  # 创建菜单栏


def __menu():
    """
    将插件入口连接到菜单栏，这样，点击菜单栏按钮时，能使用本插件功能
    :return:
    """
    window = Window
    menuBar = window.menuBar()  # 菜单栏句柄
    menu_filter = menuBar.menu_filter  # 滤镜栏句柄
    action = menuBar.newAction('色图映射', menuBar)  # 新建菜单栏选项
    action.func = newColormap  # 当按钮出发时，运行 newBlur 方法
    menu_filter.addAction(action)  # 把选项添加到 滤波 分类


def newColormap():
    window = Window  # 主窗体
    layer = window.currentLayer  # 获取当前图层句柄
    if layer:
        image = layer.image  # 当前图层QImage句柄
        mat = Converter.QImage2Numpy(image)  # 将QImage对象转化为numpy.array 对象
        controller = ColormapController(window, mat)
        controller.layer = layer
        controller.updateSlot()  # 初始更新
        if controller.exec_():
            pass
